package com.darrenchan.netease04


/**
  * 隐式转换
  */
object MyPreChange {
   implicit def dogToPerson(dog: Dog) = new Person(dog.id, dog.name)
}


object GenericTypeTest2 {

  /**
    * T是泛型，不确定什么类型，所以不一定包括compareTo方法
    *
    * Comparable 接口，包含compareTo方法
    *
    * T <: Comparable[T]    只认T是Comparable的子类
    * T <% Comparable[T]    除了会认识子类之外，还认识由其他类隐式转换过来的类（视图界定）
    *
    */
  def compare[T <% Comparable[T]](one: T, two: T): T = {
    if(one.compareTo(two) > 0) one else two
  }

  def main(args: Array[String]): Unit = {
    println(compare[String]("cc", "dd"))



    val p1 = new Person(11, "chenchi")
    val p2 = new Person(22, "szt")
    println(compare[Person](p1, p2))


    import MyPreChange._
    val d1 = new Dog(33, "tom")
    val d2 = new Dog(44, "jerry")
    println(compare[Person](d1, d2))
  }
}


class Person(val id: Int, val name: String) extends Comparable[Person] {
  override def compareTo(o: Person): Int = {
    this.id - o.id
  }

  override def toString: String = {
    id + ", " + name
  }
}

class Dog(val id: Int, val name: String) {
  override def toString: String = {
    id + ", " + name
  }
}



